# vim:ts=4:sw=4:noet:
""" New Class dialog """

import wx
import re
from c.ui.wxw.widgets.checkedentry import CheckedEntry
from c.base.support import CoreException

class NewClassDialog(wx.Dialog):

	def __init__(self, parent, classes, supername="", clsname=""):
		wx.Dialog.__init__(self, parent, -1, "New Class")
		self.mw = parent
		self.supercls = wx.Choice(self, -1, choices=classes)
		if supername!="":
			self.supercls.SetSelection(classes.index(supername))
		else:
			self.supercls.SetSelection(0)

		self.validName = re.compile("^[A-Za-z][a-zA-Z0-9]*$")
		self.entry = CheckedEntry(self, self.doCheck, self.doDone)
		
		self.ok = wx.Button(self, wx.ID_OK, "OK")
		self.ok.SetDefault()
		self.ok.Enable(False)
		cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
		
		dlgsizer = wx.BoxSizer(wx.VERTICAL)
		dlgsizer.Add(self.supercls, 0, wx.ALL | wx.EXPAND, 10)
		dlgsizer.Add(self.entry, 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
		
		btnsizer = wx.StdDialogButtonSizer()
		btnsizer.AddButton(self.ok)
		btnsizer.AddButton(cancel)
		btnsizer.Realize()
		
		dlgsizer.Add(btnsizer, 0, wx.ALL | wx.ALIGN_RIGHT, 10)
		
		self.SetSizer(dlgsizer)

		self.Fit()
		self.Centre()

	def GetValue(self):
		supercls = self.supercls.GetString(self.supercls.GetSelection())
		return (supercls, self.entry.GetValue())
	
	def doCheck(self, value):
		if value:
			cls = self.mw.controller.mem.getGlobal(value)
			if cls:
				self.ok.Enable(False)
				return False, "Class '%s' exists" % value
			if not self.validName.search(value):
				self.ok.Enable(False)
				return False, "Invalid name"
			self.ok.Enable(True)
			return True, ""
		else:
			self.ok.Enable(False)
			return False, "Need non-empty class name"

	def doDone(self):
		if self.ok.IsEnabled():
			self.EndModal(wx.ID_OK)


